<template>
    <div style="width: 100%;height: 100vh;background-color: antiquewhite;overflow: hidden">
        <div style="width:400px;margin: 100px auto">
            <div style="color: cadetblue;font-size: 30px;text-align: center;padding: 30px 0">欢迎注册</div>
            <el-form ref="registerFormRef" :model="form" :rules="rules">
                <el-form-item prop="username">
                    <el-input prefix-icon="el-icon-user-solid" v-model="form.username"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input prefix-icon="el-icon-lock" v-model="form.password" show-password></el-input>
                </el-form-item>
                <el-form-item prop="confirm">
                    <el-input prefix-icon="el-icon-lock" v-model="form.confirm" show-password></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button style="width: 100%" type="primary" @click="register">注册</el-button>
                </el-form-item>
            </el-form>

        </div>
    </div>
</template>

<script>
    import request from "../utils/request";

    export default {
        name: "Register",
        data() {
            return {
                form: {},
                rules: {
                    username: [
                        {required: true, message: '请输入用户名', trigger: 'blur'},
                    ],
                    password: [
                        {required: true, message: '请输入密码', trigger: 'blur'},
                    ],
                    confirm: [
                        {required: true, message: '请再次输入密码', trigger: 'blur'},
                    ]
                }
            }
        },
        methods: {
            register() {
                this.$refs.registerFormRef.validate(async valid => {
                    //如果没有完成校验则直接返回
                    if (!valid) {
                        this.$message({
                            type: "error",
                            message: '请按规范输入'
                        })
                        return false;
                    }
                    if (this.form.password !== this.form.confirm) {
                        this.$message({
                            type: "error",
                            message: '两次密码不一致'
                        })
                        return false;
                    }
                    const result = await request.post("/user/register", this.form)
                    if (result.code === '0') {
                        this.$message({
                            type: "success",
                            message: "注册成功！"
                        })
                        this.$router.push("/login")
                    } else {
                        this.$message({
                            type: "error",
                            message: result.msg
                        })
                    }
                });
            }
        }
    }
</script>

<style scoped>

</style>
